package com.kymatrix.mpo.plus.bpm.server.flowable.candidate.strategy;

import com.kymatrix.mpo.plus.bpm.server.flowable.candidate.BpmTaskCandidateStrategy;
import com.kymatrix.mpo.plus.bpm.server.flowable.enums.BpmTaskCandidateStrategyEnum;
import com.kymatrix.mpo.plus.bpm.server.model.entity.definition.BpmUserGroupEntity;
import com.kymatrix.mpo.plus.bpm.server.service.BpmUserGroupService;
import com.kymatrix.mpo.plus.common.util.StrUtils;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import javax.annotation.Resource;
import org.flowable.engine.delegate.DelegateExecution;
import org.springframework.stereotype.Component;

import static com.kymatrix.mpo.plus.common.util.CollectionUtils.convertSetByFlatMap;


/**
 * 用户组 {@link BpmTaskCandidateStrategy} 实现类
 *
 * @author kyle
 */
@Component
public class BpmTaskCandidateGroupStrategy implements BpmTaskCandidateStrategy {

    @Resource
    private BpmUserGroupService userGroupService;

    @Override
    public BpmTaskCandidateStrategyEnum getStrategy() {
        return BpmTaskCandidateStrategyEnum.USER_GROUP;
    }

    @Override
    public void validateParam(String param) {
        Set<String> groupIds = StrUtils.splitToStringSet(param);
        userGroupService.getUserGroupList(groupIds);
    }

    @Override
    public Set<String> calculateUsers(DelegateExecution execution, String param) {
        Set<String> groupIds = StrUtils.splitToStringSet(param);
        List<BpmUserGroupEntity> groups = userGroupService.getUserGroupList(groupIds);
        return convertSetByFlatMap(groups, BpmUserGroupEntity::getUserIds, Collection::stream);
    }

}
